﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;

    public class WaveFlowDetailDA : IWaveFlowDetailDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string LINE_ID = "@LINE_ID";
        private string SQL_CHECK_WAVE_FLOW_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@WAVE_FLOW_DETAIL WHERE WAVE_FLOW_ID = @WAVE_FLOW_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_WAVE_FLOW_DETAIL = " DELETE FROM @_@WAVE_FLOW_DETAIL WHERE WAVE_FLOW_ID = @WAVE_FLOW_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_WAVE_FLOW_DETAIL_BY_ID = " DELETE FROM @_@WAVE_FLOW_DETAIL WHERE WAVE_FLOW_ID = @WAVE_FLOW_ID  ";
        private string SQL_INSERT_WAVE_FLOW_DETAIL = " INSERT INTO @_@WAVE_FLOW_DETAIL ( WH_ID, WAVE_FLOW_ID, LINE_ID, WAVE_STEP, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @WAVE_FLOW_ID, @LINE_ID, @WAVE_STEP, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_WAVE_FLOW_DETAIL = " SELECT WFD.ROWID, WFD.WH_ID, WFD.WAVE_FLOW_ID, WFD.LINE_ID,T.TEXT AS WAVE_STEP, WFD.IS_ACTIVE, WFD.TS, WFD.CREATED_BY, WFD.CREATED_DATE, WFD.UPDATED_BY, WFD.UPDATED_DATE   FROM  @_@WAVE_FLOW_DETAIL  WFD  LEFT JOIN @_@SYS_CODE_DETAIL SCD ON SCD.SYS_CODE = WFD.WAVE_STEP AND SCD.SYS_CODE_TYPE = 'WaveSteps'  LEFT JOIN sysadm.TRANSLATION T ON 'WaveSteps.'+WFD.WAVE_STEP = T.ITEM_ID   AND  T.ITEM_TYPE = 'SYS_CODE_DETAIL' AND T.LANG+'-'+T.COUNTRY = @LANGUAGE ";
        private string SQL_UPDATE_WAVE_FLOW_DETAIL = " UPDATE @_@WAVE_FLOW_DETAIL SET WH_ID = @WH_ID, WAVE_STEP = @WAVE_STEP, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE WAVE_FLOW_ID = @WAVE_FLOW_ID AND LINE_ID = @LINE_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WAVE_FLOW_ID = "@WAVE_FLOW_ID";
        private string WAVE_STEP = "@WAVE_STEP";
        private string WH_ID = "@WH_ID";

        public bool CheckWaveFlowDetailIDUnique(string waveFlowDetailID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveFlowDetailID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_WAVE_FLOW_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckWaveFlowDetailIDUnique(string waveFlowDetailID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveFlowDetailID), new SqlParameter("@LINE_ID", lineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_WAVE_FLOW_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckWaveFlowDetailIDUnique(DataBase dataBase, DbTransaction tran, string waveFlowDetailID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveFlowDetailID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_WAVE_FLOW_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckWaveFlowDetailIDUnique(DataBase dataBase, DbTransaction tran, string waveFlowDetailID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveFlowDetailID), new SqlParameter("@LINE_ID", lineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_WAVE_FLOW_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteWaveFlowDetail(List<string> waveFlowDetailID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < waveFlowDetailID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveFlowDetailID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_WAVE_FLOW_DETAIL_BY_ID.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteWaveFlowDetail(DataBase dataBase, DbTransaction tran, List<string> waveFlowDetailID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < waveFlowDetailID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveFlowDetailID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_WAVE_FLOW_DETAIL_BY_ID.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteWaveFlowDetailByID(List<string> waveFlowDetailID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < waveFlowDetailID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveFlowDetailID[j]), new SqlParameter("@LINE_ID", lineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteWaveFlowDetailByID(DataBase dataBase, DbTransaction tran, List<string> waveFlowDetailID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < waveFlowDetailID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveFlowDetailID[j]), new SqlParameter("@LINE_ID", lineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllWaveFlowDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllWaveFlowDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public WaveFlowDetailInfo GetWaveFlowDetailByIDLineID(string waveflowdetailID, string lineID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID) + " WHERE WFD.WAVE_FLOW_ID = @WAVE_FLOW_ID AND WFD.LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveflowdetailID), new SqlParameter("@LINE_ID", lineID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            WaveFlowDetailInfo waveFlowDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    waveFlowDetailInfo = new WaveFlowDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["WAVE_FLOW_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["WAVE_STEP"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return waveFlowDetailInfo;
        }

        public WaveFlowDetailInfo GetWaveFlowDetailByIDLineID(DataBase dataBase, DbTransaction tran, string waveflowdetailID, string lineID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID) + " WHERE WFD.WAVE_FLOW_ID = @WAVE_FLOW_ID AND WFD.LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveflowdetailID), new SqlParameter("@LINE_ID", lineID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            WaveFlowDetailInfo waveFlowDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                waveFlowDetailInfo = new WaveFlowDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["WAVE_FLOW_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["WAVE_STEP"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return waveFlowDetailInfo;
        }

        public List<WaveFlowDetailInfo> GetWaveFlowDetailListByID(string waveflowdetailID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID) + " WHERE WFD.WAVE_FLOW_ID = @WAVE_FLOW_ID ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveflowdetailID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            List<WaveFlowDetailInfo> waveFlowDetailInfoList = new List<WaveFlowDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    WaveFlowDetailInfo waveFlowDetailInfo = null;
                    waveFlowDetailInfo = new WaveFlowDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["WAVE_FLOW_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["WAVE_STEP"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    waveFlowDetailInfoList.Add(waveFlowDetailInfo);
                }
            }
            return waveFlowDetailInfoList;
        }

        public List<WaveFlowDetailInfo> GetWaveFlowDetailListByID(DataBase dataBase, DbTransaction tran, string waveflowdetailID, string whLoginID, PubParasInfo pubParasInfo)
        {
            string sql = this.SQL_SELECT_ALL_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID) + " WHERE WFD.WAVE_FLOW_ID = @WAVE_FLOW_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WAVE_FLOW_ID", waveflowdetailID), new SqlParameter("@LANGUAGE", pubParasInfo.UserLanguage) };
            List<WaveFlowDetailInfo> waveFlowDetailInfoList = new List<WaveFlowDetailInfo>();
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                WaveFlowDetailInfo waveFlowDetailInfo = null;
                waveFlowDetailInfo = new WaveFlowDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["WAVE_FLOW_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["WAVE_STEP"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                waveFlowDetailInfoList.Add(waveFlowDetailInfo);
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return waveFlowDetailInfoList;
        }

        public int InsertWaveFlowDetail(WaveFlowDetailInfo waveFlowDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                waveFlowDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_WaveFlowDetail_Parameters(waveFlowDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertWaveFlowDetail(DataBase dataBase, DbTransaction tran, WaveFlowDetailInfo waveFlowDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                waveFlowDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_WaveFlowDetail_Parameters(waveFlowDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_WaveFlowDetail_Parameters(WaveFlowDetailInfo waveFlowDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.WAVE_FLOW_ID, SqlDbType.VarChar, 20), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.WAVE_STEP, SqlDbType.VarChar, 20), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            if (!string.IsNullOrEmpty(waveFlowDetailInfo.WhID))
            {
                paramArray[0].Value = waveFlowDetailInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = waveFlowDetailInfo.WaveFlowID;
            paramArray[2].Value = waveFlowDetailInfo.LineID;
            paramArray[3].Value = waveFlowDetailInfo.WaveStep;
            paramArray[4].Value = waveFlowDetailInfo.IsActive;
            paramArray[5].Value = waveFlowDetailInfo.CreatedBy;
            paramArray[6].Value = waveFlowDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateWaveFlowDetail(WaveFlowDetailInfo waveFlowDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                waveFlowDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_WaveFlowDetail_Parameters(waveFlowDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateWaveFlowDetail(DataBase dataBase, DbTransaction tran, WaveFlowDetailInfo waveFlowDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                waveFlowDetailInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_WaveFlowDetail_Parameters(waveFlowDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_WAVE_FLOW_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

